package com.example.mybatis20231226.Dao;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UserDao {
    DataSource dataSource = null;
    public UserDao(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public void addUser() throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = dataSource.getConnection();
            String sql = "insert into user values (?,?,?);";
            statement = connection.prepareStatement(sql);
            statement.setString(2, "小明");
            statement.setInt(3, 0);
            statement.execute();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
}
